#ifndef SCALEWIGET_H
#define SCALEWIGET_H


#include<QPushButton>
#include<QObject>
#include<QWidget>
#include<QSlider>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QLabel>
#include<QDebug>
#include<QPixmap>
#include<QScreen>
#include<QPainter>


class ScaleWiget : public QWidget
{
private:
    QSlider* slider;
    QSlider* sliderForWin;
    QPushButton* btnLeft;
    QPushButton* btnRight;
    QLabel* lbRight;
    QLabel* midLb;
    QVBoxLayout* vlay;
    QGridLayout* glay;
    QHBoxLayout* hlay;
    QWidget* top;

    QLabel* lb1;
    QLabel* lb2;
    QSize IconBaseSize = QSize(400,400);
public slots:
    void sliderHandler()
    {
        QScreen *screen = QGuiApplication::primaryScreen();
//        qDebug() << "draw Control 中 DPR = " << screen->devicePixelRatio();

         auto dpr = screen->devicePixelRatio();

        qDebug() << "slider.value = " << slider->value();
//        btnLeft->setIconSize(1.0*IconBaseSize*slider->value()/100);
        qDebug() << "svg dpr = " << dpr;
        QSize svgSize = 1.0*IconBaseSize*slider->value()/100;
        btnRight->setIconSize(svgSize*dpr);//伸缩svg

        //png似乎没办法这样放大缩小，svg是可以的
        auto prop = 1.0*slider->value()/100;
        qDebug() << "prop = " << prop;
        QSize newSize(prop*IconBaseSize.width(),prop*IconBaseSize.height());
        qDebug() << "newSize = " << newSize;
        QPixmap pixmap(":/pngs/resources/pancake.png");
        QPixmap scaledPixmap = pixmap.scaled(newSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
//        QPixmap scaledPixmap = pixmap.scaled(newSize, Qt::KeepAspectRatio);//不要启用平滑差值，平滑差值反而会看不到锯齿
        QIcon iconPng(scaledPixmap);
        btnLeft->setIcon(iconPng);//伸缩 png
        btnLeft->setIconSize(scaledPixmap.size());// setIconSize 必须设置，否则图标不会放大缩小

        midLb->setText(QString("png实时size  = %1 x %2 , svg实时size = %3 x %4")
                       .arg(scaledPixmap.width())
                       .arg(scaledPixmap.height())
                       .arg(svgSize.width())
                       .arg(svgSize.height()));

    }
    void scaleWindow(int sliderVal)
    {
        qDebug() << "sliderFor window Val = " << sliderVal;
        const int baseScaleFactor = 2;
        auto prop = 1.0 * sliderVal / 100;
        auto factor = prop * baseScaleFactor;
        auto newSize = factor * this->size();
        resize(newSize);


        // 遍历所有子控件，调整大小和字体
        for (QWidget *child : findChildren<QWidget *>()) {
            child->resize(child->size() * factor);
            QFont font = child->font();
            font.setPointSizeF(font.pointSizeF() * factor);
            child->setFont(font);
        }

    }
public:
    ~ScaleWiget()
    {
        delete slider;
        delete btnLeft;
        delete btnRight;
        delete vlay;
        delete glay;
    }
    ScaleWiget(QWidget* parent = nullptr) : QWidget(parent)
    {
        init();
        connect(slider,&QSlider::valueChanged,this,&ScaleWiget::sliderHandler);
        connect(sliderForWin,&QSlider::valueChanged,this,&ScaleWiget::scaleWindow);
    }
    void init()
    {
        this->setFixedSize(1200,600);
        top = new QWidget(this);

        lb1 = new QLabel(top);
        lb1->setText("png: 200*200");
        lb1->setStyleSheet("color:red; font-size:24px;");
        lb2 = new QLabel(top);
        lb2->setText("svg");
        lb2->setStyleSheet("color:red; font-size:24px;");

        slider = new QSlider(Qt::Horizontal);
        slider->setRange(1,100);
        slider->setValue(1);

        sliderForWin = new QSlider(Qt::Horizontal);
        sliderForWin->setRange(1,100);
        sliderForWin->setValue(1);

        btnLeft = new QPushButton(top);
        btnLeft->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
        btnLeft->setFixedSize(400,400);

        btnRight = new QPushButton(top);
        btnRight->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
        btnRight->setFixedSize(400,400);

//        lbRight = new QLabel(top);


        glay = new QGridLayout(top);
        glay->addWidget(lb1,0,0);
        glay->addWidget(lb2,0,1);
        glay->addWidget(btnLeft,1,0);
        glay->addWidget(btnRight,1,1);

//        lbRight->setText("egge");
//        glay->addWidget(lbRight,1,2);

//        hlay = new QHBoxLayout(top);
//        hlay->addWidget(btnLeft);
//        hlay->addWidget(btnRight);

        QIcon iconLeft(":/pngs/resources/pancake.png");//这张png，默认像素 = 200*200
//        QIcon iconRight(":/pngs/resources/pancake.png");//理论上，这样传入构造一个svg，就会触发svgIconEngine的调用的
        QIcon iconRight(":/svgs/resources/edit-delete-symbolic.svg");//理论上，这样传入构造一个svg，就会触发svgIconEngine的调用的

        auto tmpProp = 1.0*slider->value()/slider->maximum();
        qDebug() << "tmpProp = " << tmpProp;
        btnLeft->setIcon(iconLeft);
        btnLeft->setIconSize(QSize(200,200));

        btnRight->setIcon(iconRight);
//        btnRight->setIconSize(QSize(200,200));
        QScreen* screen = QGuiApplication::primaryScreen();
        auto dpr = screen->devicePixelRatio();
        qDebug() << "svg dpr = " << dpr << ",dpr*svg = " << dpr*QSize(20,20);
        btnRight->setIconSize(dpr*QSize(20,20));

//        lbRight-
//        QPixmap pixmap(":/pngs/resources/pancake.png"); // 加载资源图片
//        QPixmap svg(":/svgs/resources/pancake.svg");
//        lbRight->setPixmap(svg);
//        lbRight->setScaledContents(true);
        midLb = new QLabel("png实时size  = ");

        vlay = new QVBoxLayout(this);
        vlay->addWidget(top);
        vlay->addWidget(midLb);
        vlay->addWidget(slider);
//        vlay->addWidget(sliderForWin);

        qDebug() << "btnLeft.size = " << btnLeft->size() << ",btnLeft.iconSize = " << btnLeft->iconSize();
        qDebug() << "ScaleWidget DPR = " << this->window()->devicePixelRatio();
    }
//    void paintEvent(QPaintEvent* event) override
//    {
//        QPainter pa;
//        qDebug() << "QPainter DPR = " << pa.device()->devicePixelRatio();
//    }
};









#endif
